from rest_framework.decorators import action
from rest_framework.generics import ListAPIView, CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from goods.models import Goods, Brand, GoodsCategory, GoodsChannel, GoodsSpecification, SpecificationOption
from meiduo_admin.serializers.goods import GoodsListSerializer, GoodsBrandSerializer, GoodsCategorySerializer
# from meiduo_admin.serializers.goods import SPUSerializer
# SPUSerializer()
from meiduo_admin.utils import PageNum

class GoodsListView(ModelViewSet):

    serializer_class = GoodsListSerializer

    pagination_class = PageNum

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if keyword is '' or keyword is None:

            return Goods.objects.all()

        else:

            return Goods.objects.filter(name=keyword)

class GoodsBrandsView(ListAPIView):

    serializer_class = GoodsBrandSerializer

    queryset = Brand.objects.all()

class GoodschannelView(ListAPIView):
    serializer_class = GoodsCategorySerializer
    queryset = GoodsCategory.objects.filter(parent=None)

class GoodsCategoryView(ListAPIView):

    serializer_class = GoodsCategorySerializer

    def get_queryset(self):

        pk = int(self.kwargs['pk'])

        if pk <= 37:
            return GoodsCategory.objects.filter(parent=pk)


        else:
            return GoodsCategory.objects.filter(parent=pk)

class GoodsView(ModelViewSet):

    serializer_class = GoodsListSerializer
    pagination_class = PageNum
    queryset = Goods.objects.all()

